﻿//----------------------------------------------------
//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Data;
using System.Text;
using System.Collections.Generic;

using System.Linq;

using WinnerCMS.Common;
using WinnerCMS.Model;
using System.Data.Entity;

namespace WinnerCMS.Data
{
    /// <summary>
    /// 数据访问层CMS_ExtensionCategorie。
    /// </summary>
    public class ExtensionCategoriesDal
    {
        #region  添加数据

        /// <summary>
        /// 添加数据
        /// </summary>
        public void Add(ExtensionCategory model)
        {
            using (Entities db = new Entities())
            {
                db.Database.ExecuteSqlCommand("P_CMS_ExtensionCategories_Add {0},{1},{2}", model.Id, model.Name, model.ModelId);
            }
        }

        #endregion

        #region  修改数据

        /// <summary>
        /// 修改
        /// </summary>
        public void Edit(ExtensionCategory model)
        {
            using (Entities db = new Entities())
            {
                db.ExtensionCategores.Attach(model);
                db.Entry(model).State = EntityState.Modified;
                db.SaveChangesAsync().Wait();
            }
        }

        #endregion

        #region  删除数据

        /// <summary>
        /// 删除
        /// </summary>
        public void Delete(string id)
        {
            if (id == null) throw new ArgumentNullException("id");
            using (Entities db = new Entities())
            {
                db.Database.ExecuteSqlCommand("P_CMS_ExtensionCategories_Delete {0}", id);
            }
        }

        #endregion

        #region  加载数据

        /// <summary>
        /// 加载数据
        /// </summary>
        public ExtensionCategory GetInfo(int id)
        {
            using (var db = new Entities())
            {
                return db.ExtensionCategores.FirstOrDefault(x => x.Id == id);
            }
        }

        #endregion


        #region  加载数据

        /// <summary>
        /// 加载数据
        /// </summary>
        public IList<ExtensionCategory> BindDLL(byte modelId)
        {
            using (var db = new Entities())
            {
                return
                    db.ExtensionCategores.Where(x => x.ModelId == modelId)
                        .OrderBy(x => x.RootId)
                        .ThenBy(x => x.OrderId).ToList();
            }
        }

        #endregion

        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="modelId"></param>
        /// <returns></returns>
        public IList<ExtensionCategory> BindList(byte modelId)
        {
            using (Entities db = new Entities())
            {
                return
                    db.ExtensionCategores.Where(x => x.ModelId == modelId)
                        .OrderBy(x => x.RootId)
                        .ThenBy(x => x.OrderId)
                        .ToList();
            }

        }

    }
}